package com.yyi.springcloud.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyi.springcloud.dao.TestProdInfoValueMapper;
import com.yyi.springcloud.entities.TestProdInfoValueModel;
import com.yyi.springcloud.service.Local_TestProdInfoValueService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class Local_TestProdInfoValueImpl implements Local_TestProdInfoValueService {
	@Resource
	private TestProdInfoValueMapper testProdInfoValueMapper;

	public TestProdInfoValueMapper getTestProdInfoValueMapper() {
		return testProdInfoValueMapper;
	}

	public void setTestProdInfoValueMapper(TestProdInfoValueMapper testProdInfoValueMapper) {
		this.testProdInfoValueMapper = testProdInfoValueMapper;
	}

	@Override
	public TestProdInfoValueModel getTestProdInfoValueByCondition(TestProdInfoValueModel testProdInfoValueModel) {
		Example example = new Example(TestProdInfoValueModel.class);
		Example.Criteria criteria = example.createCriteria();
		// 是否开测
		criteria.andEqualTo("testnow", 1);
		if (testProdInfoValueModel == null) {
			return null;
		}
		// 实验室编码
		if (!StringUtils.isEmpty(testProdInfoValueModel.getLabcode())) {
			criteria.andEqualTo("labcode", testProdInfoValueModel.getLabcode());
		}
		// 台位号
		if (-1 != testProdInfoValueModel.getTestunitno()) {
			criteria.andEqualTo("testunitno", testProdInfoValueModel.getTestunitno());
		}
		// primarykey
		if (!StringUtils.isEmpty(testProdInfoValueModel.getPrimarykey())) {
			criteria.andEqualTo("primarykey", testProdInfoValueModel.getPrimarykey());
		}
		// 设置只查询指定列
		example.selectProperties("primarykey", "testunitno", "testnow", "labcode");

		List<TestProdInfoValueModel> lists = testProdInfoValueMapper.selectByExample(example);
		if (lists.size() > 0) {
			return lists.get(0);
		} else {
			return null;
		}
	}

	@Override
	public PageInfo<TestProdInfoValueModel> getAllTestProdInfoValueByCondition(
			TestProdInfoValueModel testProdInfoValueModel, int pageNum, int pageSize, String order) {
		// 分页处理
		PageHelper.startPage(pageNum, pageSize);
		Example example = new Example(TestProdInfoValueModel.class);
		Example.Criteria criteria = example.createCriteria();
		if (order != null) {
			example.setOrderByClause(order);
		}

		// 根据是否开测排序
		// example.setOrderByClause("testnow");
		// 实验室编码
		if (testProdInfoValueModel != null && !StringUtils.isEmpty(testProdInfoValueModel.getLabcode())) {
			criteria.andEqualTo("labcode", testProdInfoValueModel.getLabcode());
		}
		// 台位号
		if (testProdInfoValueModel != null && null != testProdInfoValueModel.getTestunitno()) {
			criteria.andEqualTo("testunitno", testProdInfoValueModel.getTestunitno());
		}
		// 是否在测
		if (testProdInfoValueModel != null && null != testProdInfoValueModel.getTestnow()) {
			criteria.andEqualTo("testnow", testProdInfoValueModel.getTestnow());
		}
		//注意：此处由于有分页的缘故，会发送两条sql语句，一条为select count(0),一条为select *
		//特别注意：如果PageHelper.startPage(pageNum, pageSize);中的pageNum为0，则不会执行第二条语句，此错误很难发现
		List<TestProdInfoValueModel> lists = testProdInfoValueMapper.selectByExample(example);
		PageInfo<TestProdInfoValueModel> pageInfo = new PageInfo<TestProdInfoValueModel>(lists);
		return pageInfo;
	}

	@Override
	public List<TestProdInfoValueModel> getAllTestProdInfoValueNow(TestProdInfoValueModel testProdInfoValueModel) {
		Example example = new Example(TestProdInfoValueModel.class);
		Example.Criteria criteria = example.createCriteria();
		// 根据开测台位排序
		example.setOrderByClause("testunitno");
		// 实验室编码
		if (testProdInfoValueModel != null && !StringUtils.isEmpty(testProdInfoValueModel.getLabcode())) {
			criteria.andEqualTo("labcode", testProdInfoValueModel.getLabcode());
		}
		// 台位号
		if (testProdInfoValueModel != null && null != testProdInfoValueModel.getTestunitno()) {
			criteria.andEqualTo("testunitno", testProdInfoValueModel.getTestunitno());
		}
		// 是否在测
		if (testProdInfoValueModel != null && null != testProdInfoValueModel.getTestnow()) {
			criteria.andEqualTo("testnow", testProdInfoValueModel.getTestnow());
		}
		return testProdInfoValueMapper.selectByExample(example);
	}
}
